﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Management;
using System.Text;
using System.Windows.Forms;

namespace chengz
{
    public partial class login : Form
    {
        OleDbConnection conn;

        public login()
        {
            InitializeComponent();
        }

        void fillComboBox(String sql, ComboBox comboBox)
        {
            OleDbCommand cmd = conn.CreateCommand();
            cmd.CommandText = sql;

            OleDbDataReader dr = cmd.ExecuteReader();
            comboBox.Items.Clear();
            while (dr.Read())
            {
                comboBox.Items.Add(dr["userName"] + "");
            }
        }

        private void login_Load(object sender, EventArgs e)
        {
            //主板
            string strbNumber = string.Empty;
            ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_baseboard");
            foreach (ManagementObject mo in mos.Get())
            {
                strbNumber = mo["SerialNumber"].ToString();
                break;
            }
            //if(!strbNumber.Equals(".4VHNTP2.CNWS20084G0012."))
            //{
            //    MessageBox.Show("硬件设备不匹配！");
            //    Application.Exit();
            //}


            string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Environment.CurrentDirectory + @"\chengzhongku.mdb";
            conn = new OleDbConnection(connectionString);
            try
            {
                conn.Open();
            }
            catch (Exception error)
            {
                MessageBox.Show(error.Message);
                Application.Exit();
                return;
            }

            fillComboBox("select * from cz_chengzy", comboBox_un);
        }

        void try2login()
        {
            if(comboBox_un.Text.Length == 0 || textBox_pw.Text.Length == 0)
            {
                MessageBox.Show("用户名或密码不能为空！");
                return;
            }

            OleDbCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select count(*) from cz_chengzy where userName = '" + comboBox_un.Text + "' and password = '" + textBox_pw.Text + "'";
            int rtn = (int)cmd.ExecuteScalar();
            if (rtn > 0)
            {
                new form1(comboBox_un.Text).Show();
                this.Hide();
            }
            else
                MessageBox.Show("用户名或密码错误");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try2login();
        }

        private void textBox_pw_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                try2login();
            }
        }
    }
}
